package Collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author wuhan
 * @date 2019/9/29 13:29
 */
class Compare implements Comparator<String>
{

    @Override
    public int compare(String o1, String o2) {
        if (o1.length()>o2.length())
            return 1;
        if (o1.length()<o2.length())
            return -1;
        return o1.compareTo(o2);
    }
}
public class CollectionsDemo {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("b");
        list.add("asd");
        list.add("a");
        list.add("as");
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
        System.out.println(Collections.max(list));
//        System.out.println(Collections.binarySearch(list,"b"));
        System.out.println(halfSearch(list,"aa"));

        Collections.replaceAll(list,"a","weferv");
        System.out.println(list);
    }
    public static int halfSearch(List<String> list,String key){
        int max,min,mid;
        max=list.size()-1;
        min=0;
        while (min<=max){
            mid=(min+max)>>1;
            String s = list.get(mid);
            int num = s.compareTo(key);
            if (num>0)
                max=mid-1;
            else if (num<0)
                min=mid+1;
            else return mid;

        }
        return -min-1;
    }
}
